home *** CD-ROM | disk | FTP | other *** search
- From: kanze@gabi-soft.fr (J. Kanze)
- Message-ID: <KANZE.96Mar27134634@gabi.gabi-soft.fr>
- X-Original-Date: 27 Mar 1996 12:46:34 GMT
- Path: in1.uu.net!bounce-back
- Date: 27 Mar 96 15:19:37 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: initialization of nonlocal static objects
- Organization: GABI Software, Sarl.
- References: <4j1tm6$bmu@senator-bedfellow.MIT.EDU> <9603251201.AA24987@lts.sel.alcatel.de>
- <ky3f6w679u.fsf@gator.mit.edu>
- In-Reply-To: jgealow@mtl.mit.edu's message of 26 Mar 1996 00:30:05 GMT
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMVlcweEDnX0m9pzZAQGaYwF7BsJlMeG7SBWC3E6VxFkBcW6Uq8pxbjOR
- kk3llavPj14JBa6X3l8FUghO6c8WLloX
- =AMuo
-
- In article <ky3f6w679u.fsf@gator.mit.edu> jgealow@mtl.mit.edu (Jeffrey
- C. Gealow) writes:
-
- |> In article <9603251201.AA24987@lts.sel.alcatel.de> James Kanze
- |> US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> writes:
-
- |> In article <4j1tm6$bmu@senator-bedfellow.MIT.EDU> jgealow@mtl.mit.edu
- |> (Jeffrey C. Gealow) writes:
-
- |> |> The technique described in the ARM 3.4 annotation cannot be applied
- |> |> to const objects since the values of the objects may not be changed.
-
- |> No. Const has nothing to do with the question, since objects only
- |> become const after construction.
-
- |> I don't understand James Kanze's comment. Consider:
-
- |> // file nifty_library.h:
- |> //
-
- |> class X {
- |> public:
- |> int i;
- |> X() : i(10) {};
- |> };
-
- |> extern const X obj;
-
- |> class nifty_counter {
- |> static nifty_count;
- |> public:
- |> nifty_counter() {
- |> if (nifty_count++ == 0) {
- |> obj.i = 5;
- |> }
- |> }
- |> };
-
-
- |> // file nifty_library.cc:
- |> //
-
- |> #include "nifty_library.h"
-
- |> const X obj;
-
- |> The intent of the statement obj.i = 5 in nifty_counter::nifty_counter()
- |> is to "initialize" obj. But formally, obj is initialized by the
- |> default constructor for class X.
-
- But normally, you wouldn't try and do it this way, anyway. It is rare
- that initialization is so simple, and even rarer that it only involves
- public members.
-
- The usual solution would be to provide two constructors for X, a dummy
- which does nothing, and the real one used by the nifty_counter. Thus:
-
- class X
- {
- public :
- enum DontInit { dontInit } ;
- X( DontInit ) {}
- X() : i( 5 ) {}
- private :
- int i ;
- } ;
-
- Objects to be initialized by the nifty_counter would be defined thus:
-
- X obj( X::dontInit ) ;
-
- Nifty counter then becomes:
-
- class NiftyCounter
- {
- static int cnt ;
- public :
- NiftyCounter()
- {
- if ( cnt == 0 )
- new( &obj ) X ;
- cnt ++ ;
- }
- } ;
-
- (Note that for NiftyCounter to work, it is absolutely essential that the
- definition of the object invoke a no-op constructor, since the object
- may in fact already be constructed.)
-
- As you can see, this works equally well for const objects.
- --
- James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
- GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
- Conseils en informatique industrielle --
- -- Beratung in industrieller Datenverarbeitung
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-